// 全局变量
// 域名
var currentUrl = "http://www.sdjs.top";
//var currentUrl = "http://tpay.sdjs.top";
jQuery.support.cors = true;
//ajax.全局交互
$.ajax({
    beforeSend: function () {
        $.showLoading();
    },
    complete: function () {
        $.hideLoading()
    }
});
//判断是否登陆且是否为首页，否则跳转至授权页面  
var thisUrl=parseURL(window.location.href);
if(localStorage.getItem("tel")==null&&thisUrl.file!="index.html"){
    //本地测试须注释
  window.location.href=currentUrl+"/wxsdjs/default.aspx";   
}
//解析URl函数
function parseURL(url) {  
 var a =  document.createElement('a');  
 a.href = url;  
 return {  
 source: url,  
 protocol: a.protocol.replace(':',''),  
 host: a.hostname,  
 port: a.port,  
 query: a.search,  
 params: (function(){  
     var ret = {},  
         seg = a.search.replace(/^\?/,'').split('&'),  
         len = seg.length, i = 0, s;  
     for (;i<len;i++) {  
         if (!seg[i]) { continue; }  
         s = seg[i].split('=');  
         ret[s[0]] = s[1];  
     }  
     return ret;  
 })(),  
 file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],  
 hash: a.hash.replace('#',''),  
 path: a.pathname.replace(/^([^\/])/,'/$1'),  
 relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],  
 segments: a.pathname.replace(/^\//,'').split('/')  
 };  
}    


$('.mask,.bg').bind("touchmove", function (e) {
    e.preventDefault();
});

$("#select_city").on('click', function () {
    $.alert("暂未开放其他城市")
})
// 移动端取消滚动条是达不到效果的，这时就需要去除遮罩层和按钮层的touchmove的默认事件



//打开个人中心                                                                                                  
$("#open_center").click(function () {
    if (localStorage.getItem("tel") == null) {
        return false
        //获得登陆标记，否则阻止行为;
    } else {
        $('.mask').css({
            "display": "block"
        }).children("#user_center").stop().animate({
            "left": "0rem"
        }, 600).css({
            "display": "block"
        });
    }

    var userInfo = localStorage.getItem("userInfo");
    var userInfoObj = JSON.parse(userInfo);
    $("#get_phone").html(userInfoObj.data.Phone);



    //获取优惠券数量；      
    $.ajax({
        type: "get",
        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_YouHuiQuan",
        dataType: "json",
        data: {
            "UserId": userInfoObj.data.UserId
        },
        success: function (data) {
            //console.log(data);
            var couponLen = data.data.length;
            $("#coupon_num").html(couponLen + "张可用");
        },
        error: function () {
            return false;
        }
    });

    //获取余额；       
    $.ajax({
        type: "get",
        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_UserMoney",
        dataType: "json",
        data: {
            "UserId": userInfoObj.data.UserId
        },
        success: function (data) {
            //console.log(data);
            if (data.msgcode == "1") {
                $("#balance_money").html(data.data.Money + "元");
            } else {
                return false
            }
        },
        error: function () {
            return false;
        }
    });

});
//退出个人中心  
$('.bg').click(function () {
    if ($('#user_center').css("display") == "block") {
        $("#user_center").stop().animate({
            "left": "-5.8rem"
        }, 600, function () {
            $("#user_center").css({ 'display': "none" }).parent('.mask').css({ "display": "none" })
        })
    }
})
//退出登录
$('#quit').click(function () {
    localStorage.removeItem("tel");
    localStorage.clear();
    location.reload();
    window.location.href = currentUrl + "/WxSdjs/index.html"; //上传服务器上面用
})
$("#product_mes").on("keydown", inputLimit);

//限制文本框输入
function inputLimit() {
    var len = $(this).val().length;
    var maxLen = $('.limit_right').html()
    var num = len;
    if (len > maxLen - 1) {
        $(this).val($(this).val().substring(0, maxLen));
        $(".limit_input .limit_left").html(maxLen);
    } else {
        $(".limit_input .limit_left").html(num);
    }
}
//限制手机号长度
function inputLimitTel() {
    var len = $(this).val().length;
    if (len > 11) {
        $(this).val($(this).val().substring(0, 11))
    }
}

function alertWrong() {
    $(".mask").css("display", "block");
    var alertStr;
}

$("#go_back").click(function () {
    window.location.href = document.referrer; //返回上一页;同时刷新页面
})
